package com.uzai.console.service.uzaichat.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.uzaichat.kefuclient.KefuClientAddDto;
import com.uzai.console.dto.uzaichat.kefuclient.KefuClientIdDto;
import com.uzai.console.dto.uzaichat.kefuclient.KefuClientQuery;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.KefuClient;
import com.uzai.console.entity.KefuUser;
import com.uzai.console.mapper.KefuClientMapper;
import com.uzai.console.mapper.KefuUserMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.uzaichat.KefuClientService;
import com.uzai.console.vo.uzaichat.kefuclient.KefuClientInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年09月01日 19:26
 */
@Service
public class KefuClientServiceImpl implements KefuClientService {

    @Autowired
    private KefuClientMapper kefuClientMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private KefuUserMapper kefuUserMapper;

    /**
     * 分页查询机器人绑定客服关系列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuClientQuery
     * @return null
     */
    public Page<KefuClientInfoVo> findKefuClientList(KefuClientQuery kefuClientQuery){
        //新建返回到页面的LIST对象
        List<KefuClientInfoVo> kefuClientInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<KefuClient> page = new Page<>();
        kefuClientQuery.convert(page);
        List<KefuClient> kefuClientList = kefuClientMapper.findByList(page, kefuClientQuery);

        if(kefuClientList != null){
            for (KefuClient kefuClient : kefuClientList){
                KefuClientInfoVo kefuClientInfoVo = new KefuClientInfoVo();
                BeanUtils.copyProperties(kefuClient, kefuClientInfoVo);

                //查询机器人信息
                if(kefuClient.getDeviceUniqueId() != null && StringUtils.isNotBlank(kefuClient.getDeviceType())){
                    Device device = deviceService.findDeviceInfoByDeviceUniqueId(kefuClient.getDeviceUniqueId(), kefuClient.getDeviceType(), kefuClient.getMerId());
                    if(device != null){
                        kefuClientInfoVo.setDeviceId(device.getDeviceId());
                        kefuClientInfoVo.setDeviceName(device.getDeviceName());
                        kefuClientInfoVo.setDeviceRemark(device.getDeviceRemark());
                    }
                }

                //查询客服账号信息
                if(kefuClient.getKefuUserId() != null){
                    KefuUser kefuUser = kefuUserMapper.selectById(kefuClient.getKefuUserId(), kefuClient.getMerId());
                    if(kefuUser != null){
                        kefuClientInfoVo.setUser(kefuUser.getUser());
                        kefuClientInfoVo.setName(kefuUser.getName());
                    }
                }

                kefuClientInfoVoList.add(kefuClientInfoVo);
            }
        }

        //返回到页面分页对象
        Page<KefuClientInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(kefuClientInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }


    /**
     * 查询机器人绑定客服关系详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuClientIdDto
     * @return null
     */
    public KefuClientInfoVo findKefuClientInfo(KefuClientIdDto kefuClientIdDto){
        if(kefuClientIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        KefuClient kefuClient = kefuClientMapper.selectById(kefuClientIdDto.getId(), kefuClientIdDto.getMerId());

        if(kefuClient == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该机器人绑定客服关系不存在");
        }

        KefuClientInfoVo kefuClientInfoVo =  new KefuClientInfoVo();

        BeanUtils.copyProperties(kefuClient, kefuClientInfoVo);

        return kefuClientInfoVo;
    }


    /**
     * 添加机器人绑定客服关系
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuClientAddDto
     * @return null
     */
    @Transactional
    public void addKefuClient(KefuClientAddDto kefuClientAddDto){
        if(kefuClientAddDto.getDeviceUniqueIdList() == null || kefuClientAddDto.getDeviceUniqueIdList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择机器人");
        }

        if(kefuClientAddDto.getKefuUserId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择客服账号");
        }

        if(StringUtils.isBlank(kefuClientAddDto.getDeviceType())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入机器人类型");
        }

        for (Long deviceUniqueId : kefuClientAddDto.getDeviceUniqueIdList()){
            //先删除旧绑定关系，在添加新的
            kefuClientMapper.deleteByDeviceUniqueId(deviceUniqueId, kefuClientAddDto.getMerId());

            //新添加
            KefuClient kefuClient = new KefuClient();
            kefuClient.setMerId(kefuClientAddDto.getMerId());
            kefuClient.setDeviceUniqueId(deviceUniqueId);
            kefuClient.setKefuUserId(kefuClientAddDto.getKefuUserId());
            kefuClient.setDeviceType(kefuClientAddDto.getDeviceType());
            Integer now = DateUtil.getNowTime();
            kefuClient.setId(IdWorker.getId());
            kefuClient.setCreateTime(now);
            kefuClient.setUpdateTime(now);
            kefuClientMapper.insertSelective(kefuClient);
        }
    }

    /**
     * 删除机器人绑定客服关系
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param kefuClientIdDto
     * @return null
     */
    public void deleteKefuClient(KefuClientIdDto kefuClientIdDto){

        if(kefuClientIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择id");
        }

        //删除机器人绑定客服关系
        kefuClientMapper.deleteById(kefuClientIdDto.getId(), kefuClientIdDto.getMerId());
    }

}
